﻿namespace Web.Control.WWW.Contacts
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;
    using System.Collections.Generic;

    using Lx.Web.Common.ControlsUI;
    using BLF.WWW_WWW.Contact;
    using Lx.Web.Common.UI;    
    
    /// <summary>
    /// MyContacts 的摘要说明
    /// </summary>
    public class MyContacts : BaseControl
    {
        /// <summary>
        /// 好友数量
        /// </summary>
        protected int FriendCount = 0;
        /// <summary>
        /// 接受请求数量
        /// </summary>
        protected int ReceiveCount = 0;
        /// <summary>
        /// 发送请求数量
        /// </summary>
        protected int SendCount = 0;
        /// <summary>
        /// 绑定有朋友列表
        /// </summary>
        protected Repeater rfList;
        /// <summary>
        /// 绑定无朋友列表
        /// </summary>
        protected Repeater rnofList;
        protected Literal litScript;
        /// <summary>
        /// 问题列表
        /// </summary>
        protected List<Model.WWW_WWW.Answers.QuestionModel> QList;
        //当前显示会员类型(2为请求列表，1为好友列表，0为推荐人列表)
        protected int showUserType = -1;
        protected string memberno = "";
        private int pageIndex = 1;
        private int pageSize = 10;
        /// <summary>
        /// 操作联系人标识符（0不存在1已经是好友2操作成功3操作失败）
        /// </summary>
        protected int opMessage=-1;

        protected override void OnLoad(EventArgs e)
        {            
            memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                Page.Response.Redirect("/user/login.htm?ReturnUrl=" + Page.Request.Url.AbsoluteUri);
                return;
            }
            if (Page.Request.QueryString["MemberNo"] != null && Page.Request.QueryString["opMemberNo"] != null)
            {
                string BuddyMemberNo = Page.Request.QueryString["MemberNo"].Trim();  //对方
                string MyMemberNo = Page.Request.QueryString["opMemberNo"].Trim(); //自己

                if (MyMemberNo == memberno)
                {
                    bool op = false;
                    if (Page.Request.QueryString["accept"] != null)
                    {
                        op = true;
                    }
                    else if (Page.Request.QueryString["reject"] == null)
                    {
                        Page.Response.Redirect("/default.htm");
                        return;
                    }
                    Model.CommonMessage cMessage = Contacts.OpContacts(MyMemberNo, BuddyMemberNo, op);
                    if (cMessage.State)
                    {
                        opMessage = cMessage.Effect;

                        if (cMessage.Effect == 2)
                        {
                            string[] strList = cMessage.Msg.Split(new char[] { '|', '|' });
                            RqstSendMail(op, strList[4], strList[6], MyMemberNo, BuddyMemberNo, strList[0], strList[2]);
                        }
                    }
                    else
                    {
                        opMessage = 3;
                    }
                }
            }
            if (Request["op"] != null && !String.IsNullOrEmpty(Request["op"].ToString()))
            {
                showUserType = 2;
            }
            ArrayList outList;
            List<Model.WWW_User.User.UserSummaryInfoModel> userList;
            Model.CommonMessage commonMessage = Contacts.GetMyContacts(memberno, pageIndex, pageSize, out outList);
            FriendCount = (int)outList[0];
            ReceiveCount = (int)outList[1];
            SendCount = (int)outList[2];
            userList = outList[3] as List<Model.WWW_User.User.UserSummaryInfoModel>;
            if (FriendCount > 0)
            {
                QList = outList[4] as List<Model.WWW_WWW.Answers.QuestionModel>;
                if (rfList != null && showUserType != 2)
                {
                    showUserType = 1;
                    rfList.DataSource = userList;
                    rfList.DataBind();
                }
            }
            else {
                if (rnofList != null && showUserType != 2)
                {
                    showUserType=0;
                    rnofList.DataSource = userList;
                    rnofList.DataBind();
                }
            }            
            if (litScript != null)
            {
                litScript.Text = "<script>var Memberno='" + memberno+"';";
                litScript.Text += "var FriendCount=" + FriendCount;                
                litScript.Text += ";var ReceiveCount=" + ReceiveCount;
                litScript.Text += ";var SendCount=" + SendCount;
                litScript.Text += ";var ShowUserType=" + showUserType;
                litScript.Text += ";var userPage=" + (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(new BLF.PageableObject(FriendCount, pageIndex, pageSize)) + ";</script>";
            }
            base.OnLoad(e);
        }

        private void RqstSendMail(bool nOP, string FromName, string ToName, string FromID, string ToID, string ToEmail, string ToLng)
        {
            if (ToEmail != "")
            {
                string sSubject = "";
                string sBody = "";
                string eSubject = "";
                string eBody = "";

                if (ToLng == "") ToLng = "en-us";
                if (nOP) // Accept the request
                {
                    sSubject = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/InvitationAcceptSite.xml", ToLng)), ToLng), "email", "subject", "");
                    sBody = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/InvitationAcceptSite.xml", ToLng)), ToLng), "email", "body", "");
                    eSubject = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/InvitationAccept.xml", ToLng)), ToLng), "email", "subject", "");
                    eBody = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/InvitationAccept.xml", ToLng)), ToLng), "email", "body", "");
                }
                else // Reject the request
                {
                    sSubject = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/InvitationRejectSite.xml", ToLng)), ToLng), "email", "subject", "");
                    sBody = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/InvitationRejectSite.xml", ToLng)), ToLng), "email", "body", "");
                    eSubject = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/InvitationReject.xml", ToLng)), ToLng), "email", "subject", "");
                    eBody = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/InvitationReject.xml", ToLng)), ToLng), "email", "body", "");
                }

                sSubject = sSubject.Replace("{0}", FromName);
                sBody = sBody.Replace("{0}", FromName).Replace("{1}", ToName).Replace("{2}", FromID).Replace("{3}", ToID);
                eSubject = eSubject.Replace("{0}", FromName);
                eBody = eBody.Replace("{0}", FromName).Replace("{1}", ToName).Replace("{2}", FromID).Replace("{3}", ToID);

                BLF.WWW_User.UserMail.Mail.WriteInform(ToID, FromID, sSubject, sBody);
                Lx.Web.Common.UI.MsMailSend.SendMail(eSubject, eBody, ToEmail, null, true);
            }
        }
        
    }
}
